import { RcFile } from 'antd/lib/upload';
import { NegotiationOperateType } from './types.d';

export const mapOperateType = (type: NegotiationOperateType) => {
  switch (type) {
    case NegotiationOperateType.USER_CREATE_COMPLAINT:
      return '用户提交投诉';
    case NegotiationOperateType.USER_CONTINUE_COMPLAINT:
      return '用户继续投诉';
    case NegotiationOperateType.USER_RESPONSE:
      return '用户留言';
    case NegotiationOperateType.PLATFORM_RESPONSE:
      return '平台留言';
    case NegotiationOperateType.MERCHANT_RESPONSE:
      return '商户留言';
    case NegotiationOperateType.MERCHANT_CONFIRM_COMPLETE:
      return '商户申请结单';
    case NegotiationOperateType.COMPLAINT_PARTIAL_REFUNDED_SYSTEM_MESSAGE:
      return '投诉单发起部分退款系统通知';
    case NegotiationOperateType.USER_CREATE_COMPLAINT_SYSTEM_MESSAGE:
      return '用户提交投诉系统通知';
    case NegotiationOperateType.COMPLAINT_FULL_REFUNDED_SYSTEM_MESSAGE:
      return '投诉单发起全额退款系统通知';
    case NegotiationOperateType.USER_CONTINUE_COMPLAINT_SYSTEM_MESSAGE:
      return '用户继续投诉系统通知';
    case NegotiationOperateType.COMPLAINT_REFUND_RECEIVED_SYSTEM_MESSAGE:
      return '投诉单退款到账系统通知';
    case NegotiationOperateType.USER_REVOKE_COMPLAINT:
      return '用户主动撤诉';
    case NegotiationOperateType.PLATFORM_HELP_APPLICATION:
      return '平台催办';
    case NegotiationOperateType.USER_APPLY_PLATFORM_HELP:
      return '申请协助';
    case NegotiationOperateType.MERCHANT_APPROVE_REFUND:
      return '商户同意退款申请';
    case NegotiationOperateType.MERCHANT_REFUSE_RERUND:
      return '商户拒绝退款申请';
    case NegotiationOperateType.USER_SUBMIT_SATISFACTION:
      return '用户提交满意度调查结果';
    case NegotiationOperateType.SERVICE_ORDER_CANCEL:
      return '用户取消服务订单';
    case NegotiationOperateType.SERVICE_ORDER_COMPLETE:
      return '用户完成服务订单';
    default:
      return '未知类型';
  }
};

const fileToDataURL = (file: Blob): Promise<any> => {
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.onloadend = (e) => resolve((e.target as FileReader).result);
    reader.readAsDataURL(file);
  });
};
const dataURLToImage = (dataURL: string): Promise<HTMLImageElement> => {
  return new Promise((resolve) => {
    const img = new Image();
    img.onload = () => resolve(img);
    img.src = dataURL;
  });
};
const canvastoFile = (
  canvas: HTMLCanvasElement,
  type: string,
  quality: number,
): Promise<Blob | null> => {
  return new Promise((resolve) => canvas.toBlob((blob) => resolve(blob), type, quality));
};
/**
 * 图片压缩方法
 * @param {Object}  file 图片文件
 * @param {String} type 想压缩成的文件类型
 * @param {Nubmber} quality 压缩质量参数
 * @returns 压缩后的新图片
 */
export const compressionFile = async (file: RcFile, quality = 0.3) => {
  const fileName = file.name;
  const canvas = document.createElement('canvas');
  const context = canvas.getContext('2d') as CanvasRenderingContext2D;
  const base64 = await fileToDataURL(file);
  const img = await dataURLToImage(base64);
  canvas.width = img.width;
  canvas.height = img.height;
  context.clearRect(0, 0, img.width, img.height);
  context.drawImage(img, 0, 0, img.width, img.height);
  const blob = (await canvastoFile(canvas, file.type, quality)) as Blob; // quality:0.5可根据实际情况计算
  const newFile = await new File([blob], fileName, {
    type: file.type,
  });
  return newFile;
};

export const getCompressionFileUrl = async (file: RcFile) => {
  const cFile = await compressionFile(file);
  // console.log('源文件', file);
  // console.log('压缩后文件', cFile);
  const minFile = cFile.size < file.size ? cFile : file;
  return await fileToDataURL(minFile);
};
